<?php
class v1_finance{
    function index(){
    	$data['basic']=$this->yum->config['Manage']['domain'].'?c=finance&a=index';
    	$data['excel']=$this->yum->config['Manage']['domain'].'?c=finance&a=excel';
		$params=array('limit'=>array(intval($this->yum->get('start')),15));
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('deduction','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][('A.'.$data['order'])]=$data['rule'];
				$data['excel'].='&order='.$data['order'].'&rule='.$data['rule'];
			}
		}else{
			$data['order']='date';$data['rule']='desc';
			$params['order']['A.'.$data['order']]=$data['rule'];
			$data['excel'].='&order='.$data['order'].'&rule='.$data['rule'];
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('A.date >='=>strtotime($data['date'][0]),'A.date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
				$data['excel'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
					$data['excel'].='&locked[]='.$value;
				}
				$params['where']['A.locked IN']=$data['locked'];
			}
		}else{
			$data['locked'][1]=1;
			$params['where']['A.locked=']=1;
			$data['excel'].='&locked[]=1';
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='account'){
					$params['where']['A.account LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='mail'){
					$params['where']['B.mail LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='name'){
					$params['where']['A.name LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
				$data['excel'].='&tab='.$data['tab'];
				$data['excel'].='&search='.urlencode($data['search']);
			}
		}
    	$payment=$this->yum->model('payment');
    	$data['total']=$payment->GPaymentFull(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
    	$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$data['payment']=$payment->GPaymentFull($params);
		$Corr=array();
		foreach($data['payment'] as $key=>$value){
			$Corr[$value['company']]=$value['company'];
			$Corr[$value['province']]=$value['province'];
			$Corr[$value['city']]=$value['city'];
		}
		if(empty($Corr)){
			$data['sort']=array();
		}else{
			$sort=$this->yum->model('sort');
			$data['sort']=$sort->GSort(array('where'=>array('id IN'=>$Corr)));
		}
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function excel(){
		$params=array();
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('deduction','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][('A.'.$data['order'])]=$data['rule'];
			}
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('A.date >='=>strtotime($data['date'][0]),'A.date <='=>strtotime($data['date'][1]));
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				$params['where']['A.locked IN']=$temp;
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='account'){
					$params['where']['A.account LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='mail'){
					$params['where']['B.mail LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='name'){
					$params['where']['A.name LIKE']='%'.$data['search'].'%';
				}
			}
		}
		$payment=$this->yum->model('payment');
		$payment=$payment->GPaymentFull($params);
		$Corr=array();
		foreach($payment as $key=>$value){
			$Corr[$value['company']]=$value['company'];
			$Corr[$value['province']]=$value['province'];
			$Corr[$value['city']]=$value['city'];
		}
		if(empty($Corr)){
			$sort=array();
		}else{
			$sort=$this->yum->model('sort');
			$sort=$sort->GSort(array('where'=>array('id IN'=>$Corr)));
		}
		$excel=$this->yum->library('PHPExcel');
		$excel->getProperties()->setTitle("Office 2007 XLSX Document");
		$excel->getProperties()->setSubject("Office 2007 XLSX Document");
		$excel->getProperties()->setDescription("document for Office 2007 XLSX, generated using phpexcel.");
		$excel->setActiveSheetIndex(0);
		$excel->getActiveSheet()->getPageMargins()->setTop(0);
		$excel->getActiveSheet()->getPageMargins()->setRight(0.6);
		$excel->getActiveSheet()->getPageMargins()->setLeft(0.6);
		$excel->getActiveSheet()->getPageMargins()->setBottom(0);
		$objActSheet=$excel->getActiveSheet();
		$objActSheet->setTitle('佣金支付表');
		$objActSheet->getColumnDimension('D')->setWidth(25);
		$objActSheet->setCellValueByColumnAndRow(0,1,'成员');
		$objActSheet->setCellValueByColumnAndRow(1,1,'银行');
		$objActSheet->setCellValueByColumnAndRow(2,1,'地区');
		$objActSheet->setCellValueByColumnAndRow(3,1,'银行账户');
		$objActSheet->setCellValueByColumnAndRow(4,1,'姓名');
		$objActSheet->setCellValueByColumnAndRow(5,1,'税前');
		$objActSheet->setCellValueByColumnAndRow(6,1,'缴税');
		$objActSheet->setCellValueByColumnAndRow(7,1,'手续费');
		$objActSheet->setCellValueByColumnAndRow(8,1,'实际支付');
		$objActSheet->setCellValueByColumnAndRow(9,1,'支付日期');
		$objActSheet->setCellValueByColumnAndRow(10,1,'状态');
		$line=2;
		foreach($payment as $value){
			$objActSheet->setCellValueByColumnAndRow(0,$line,$value['mail']);
			$objActSheet->setCellValueByColumnAndRow(1,$line,$sort[$value['company']]['name']);
			$objActSheet->setCellValueByColumnAndRow(2,$line,$sort[$value['province']]['name']);
			$objActSheet->getStyle('D'.$line)->getNumberFormat()
			->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
			$objActSheet->setCellValueByColumnAndRow(3,$line,$value['account']);
			$objActSheet->setCellValueByColumnAndRow(4,$line,$value['name']);
			$objActSheet->setCellValueByColumnAndRow(5,$line,$value['deduction']);
			$objActSheet->setCellValueByColumnAndRow(6,$line,$value['tax']);
			$objActSheet->setCellValueByColumnAndRow(7,$line,$value['outlay']);
			$objActSheet->setCellValueByColumnAndRow(8,$line,$value['fact']);
			$objActSheet->setCellValueByColumnAndRow(9,$line,date('Y-m-d',$value['date']));
			$objActSheet->setCellValueByColumnAndRow(10,$line,$value['locked']==0?'已支付':'待支付');
			$line++;
		}
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="'.(date('Y-m-d').'-finance-'.rand()).'.xlsx"');
		header('Cache-Control: max-age=0');
		$objWriter=PHPExcel_IOFactory::createWriter($excel,'Excel2007');
		$objWriter->save('php://output');
    }
    function locked(){
		$output=array('result'=>0,'msg'=>'');
		$payment=$this->yum->model('payment');
		$information=$this->yum->model('information');
		$id=$this->yum->get('id');
		$locked=intval($this->yum->get('locked',true));
		if(is_array($id)){$pay=$id;}else{$pay=array($id);}
		$pay=array_map('intval',$pay);
		$mation=$payment->GPayment(array('where'=>array('id IN'=>$pay)));$check=true;$action=$locked==0?'frozen=frozen-':'frozen=frozen+';
		$this->yum->library('mysql')->startTrans();
		foreach($mation as $value){
			$affected=$information->UMation(array('set'=>array($action=>$value['deduction']),'where'=>array('member='=>$value['member'])));
			if($affected!=1){
				$check=false;break;
			}
		}
		if($check){
			$affected=$payment->UPayment(array('where'=>array('id IN'=>$pay),'set'=>array('locked='=>$locked)));
			if($affected==count($pay)){
				$this->yum->library('mysql')->commit();
				if(is_array($id)){
					$output['result']=2;
				}else{
					$output['result']=5;
					$output['td']=array('offset'=>11,'data'=>array($locked==0?'已支付':'待支付'));
					$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?2:0).'}'),'text'=>($locked==0?'撤销':'支付'));
				}
			}else{
				$this->yum->library('mysql')->rollback();
				if(is_array($id)){
					$output['msg']='重复的数据提交.';
				}else{
					$output['msg']='状态已变更.';
				}
			}
		}else{
			$output['msg']='数据库更新错误.';
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
}
?>